﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Core.Library;
using DataObject.Category;
using Microsoft.ApplicationBlocks.Data;

namespace DataObject.Article
{
    public class ArticleDAL
    {
        /***************************************
        = Back End
        -------------------------------------- */
        public static IDataReader Article_GetListSearch(int categoryId, string keyword, int status, int pageIndex, int pageSize, ref int totalRecords)
        {
            var filter = "1=1";
            var strId = string.Empty;

            // CategoryId
            var o = CategoryController.Category_GetById(categoryId);
            if (o != null)
            {
                if (o.ParentId == Constants.PARENT_ROOT)
                {
                    var lstId = new List<int>();
                    var lstCategory = CategoryController.Category_GetByParentId((int)CategoryType.Article, categoryId, Constants.ACTIVE);
                    if (lstCategory != null && lstCategory.Count > 0)
                        lstId.AddRange(lstCategory.Select(t => t.CategoryId));

                    if (lstId.Count > 0)
                        strId = lstId.Aggregate(strId, (current, t) => current + t + " ,");

                    if (!string.IsNullOrEmpty(strId))
                    {
                        strId = strId.Substring(0, strId.Length - 2);
                        filter += string.Format(" AND CategoryId in ({0})", strId);
                    }
                }
                else
                    filter += string.Format(" AND CategoryId = {0}", categoryId);
            }            
           
            // Status
            switch (status)
            {
                case StatusHelper.IsWaitingForPublish:
                    filter += string.Format(" AND [Status] & {0} = {0}", status);
                    break;
                case StatusHelper.IsPublished:
                    filter += string.Format(" AND [Status] & {0} = {0}", status);
                    break;
                default:
                    filter += string.Format(" AND ([Status] & {0} = {0} OR [Status] & {1} = {1})", StatusHelper.IsWaitingForPublish, StatusHelper.IsPublished);
                    break;
            }
           
            // Keyword
            if (!string.IsNullOrEmpty(keyword))
                filter += string.Format(" AND Title LIKE N'%{0}%'", keyword);
                      
            totalRecords = Action.GetListPagingCount("Article", filter);
            return Action.Reader_GetListPaging("Article", "PublishOn Desc", filter, pageIndex, pageSize);             
        }

        public static int Article_Create(ArticleObject o)
        {
            return (int)SqlHelper.ExecuteScalar(Constants.ConnectionString, "Article_Create", 
                                    o.CategoryId, o.Title, o.Sapo, o.Body, o.Source, o.ImgAvatar, o.PublishOn, o.CreatedBy, o.Status);
        }        

        public static void Article_Update(ArticleObject o)
        {
            SqlHelper.ExecuteNonQuery(Constants.ConnectionString, "Article_Update",
                        o.ArticleId, o.CategoryId, o.Title, o.Sapo, o.Body, o.Source, o.ImgAvatar, o.EditedBy, o.ApprovedBy, o.PublishOn, o.Status);
        }

        public static void Article_Delete(int id)
        {
            SqlHelper.ExecuteNonQuery(Constants.ConnectionString, "Article_Delete", id);
        }

        public static IDataReader Article_GetById(int articleId)
        {
            return SqlHelper.ExecuteReader(Constants.ConnectionString, "Article_GetById", articleId);
        }
              
        public static int Article_GetStatistic(int adminUserId, int status)
        {
            return (int)SqlHelper.ExecuteScalar(Constants.ConnectionString, "Article_GetStatistic", adminUserId, status);
        }

        public static void Article_Update_Status(int adminUserId, int articleId, int status)
        {
            SqlHelper.ExecuteNonQuery(Constants.ConnectionString, "Article_Update_Status", adminUserId, articleId, status);
        }

        /***************************************
        = Front End
        -------------------------------------- */
        public static IDataReader Article_GetListByCategory_Static(int categoryId)
        {
            return SqlHelper.ExecuteReader(Constants.ConnectionString, "Article_GetListByCategory_Static", categoryId);
        }

        public static IDataReader Article_GetListByCategoryId_Parent(int categoryId)
        {
            return SqlHelper.ExecuteReader(Constants.ConnectionString, "Article_GetListByCategoryId_Parent", categoryId);
        }

        public static IDataReader Article_GetListByCategoryId(int categoryId)
        {
            return SqlHelper.ExecuteReader(Constants.ConnectionString, "Article_GetListByCategoryId", categoryId);
        }

        public static IDataReader Article_GetTopHighlight(int count)
        {
            var filter = string.Format("([Status] & 64 = 64) AND PublishOn <= '{0}'", DateTime.Now);
            return Action.Reader_GetListPaging("Article", "PublishOn Desc", filter, SettingValues.PageIndex, count);            
        }

        public static IDataReader Article_GetListOrderByPublishOn(int categoryId, int pageIndex, int pageSize, ref int totalRecords)
        {
            var filter = "1=1";
            var strId  = string.Empty;

            // CategoryId
            var o = CategoryController.Category_GetById(categoryId);
            if (o != null)
            {
                if (o.ParentId == Constants.PARENT_ROOT)
                {
                    var lstId = new List<int>();
                    var lstCategory = CategoryController.Category_GetByParentId((int)CategoryType.Article, categoryId, Constants.ACTIVE);
                    if (lstCategory != null && lstCategory.Count > 0)
                        lstId.AddRange(lstCategory.Select(t => t.CategoryId));

                    if (lstId.Count > 0)
                        strId = lstId.Aggregate(strId, (current, t) => current + t + " ,");

                    if (!string.IsNullOrEmpty(strId))
                    {
                        strId = strId.Substring(0, strId.Length - 2);
                        filter += string.Format(" AND CategoryId in ({0})", strId);
                    }
                }
                else
                    filter += string.Format(" AND CategoryId = {0}", categoryId);
            }

            filter += string.Format("AND ([Status] & 8 = 8 OR [Status] & 64 = 64) AND PublishOn <= '{0}'", DateTime.Now);            

            totalRecords = Action.GetListPagingCount("Article", filter);
            return Action.Reader_GetListPaging("Article", "PublishOn Desc", filter, pageIndex, pageSize);            
        }
    }
}
